package com.msk.web.controller.wlhy.goods;

import com.msk.base.domain.goods.BaseDistance;
import com.msk.base.service.goods.BaseDistanceService;
import com.msk.common.core.controller.BaseController;
import com.msk.common.core.domain.R;
import com.msk.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "运距接口")
@RestController
@RequestMapping("/api/distance")
public class BaseDistanceController extends BaseController {

    @Autowired
    private BaseDistanceService baseDistanceService ;

    /**
     * @Description 新增运距
     * @Author wlxiang
     * @Date 16:54 2023/6/9
     * @Param 
     * @param baseDistance
     * @return com.msk.common.core.domain.R<?>
     **/
    @ApiOperation("新增运距")
    @PostMapping("/addBaseDistance")
    public R<?> addBaseDistance(@RequestBody BaseDistance baseDistance) {
        String affiliatedPersonId = baseDistance.getAffiliatedPersonId();
        if(StringUtils.isBlank(affiliatedPersonId)){
            return R.appFail(400101, "请求参数所属人ID#affiliatedPersonId为空");
        }
        baseDistance.preInsert();
        int rows = baseDistanceService.insert(baseDistance);
        if(!(rows > 0) ){
            return R.appFail(400101,"新增运距失败");
        }
        return R.appOk("新增运距成功");
    }

    /**
     * @Description updateBaseDistance
     * @Author wlxiang
     * @Date 17:02 2023/6/9
     * @Param 
     * @param baseDistance
     * @return com.msk.common.core.domain.R<?>
     **/
    @ApiOperation("编辑运距")
    @PostMapping("/updateBaseDistance")
    public R<?> updateBaseDistance(@RequestBody BaseDistance baseDistance) {
        String id = baseDistance.getId();
        if(StringUtils.isBlank(id)){
            return R.appFail(400101, "请求参数运距ID#运距为空");
        }
        baseDistance.preInsert();
        int rows = baseDistanceService.update(baseDistance);
        if(!(rows > 0) ){
            return R.appFail(400101,"编辑运距失败");
        }
        return R.appOk("编辑运距成功");
    }

    /**
     * @Description 删除运距
     * @Author wlxiang
     * @Date 17:15 2023/6/9
     * @Param 
     * @param params
     * @return com.msk.common.core.domain.R<?>
     **/
    @ApiOperation("删除运距")
    @PostMapping("/deleteBaseDistance")
    public R<?> deleteBaseDistance(@RequestBody Map<String,String> params) {
        String id = params.get("id");
        if(StringUtils.isBlank(id)){
            return R.appFail(400101, "请求参数运距ID#运距为空");
        }
        int rows = baseDistanceService.delete(id);
        if(!(rows > 0) ){
            return R.appFail(400101,"删除运距失败");
        }
        return R.appOk("删除运距成功");
    }

    /**
     * @Description 运距数据列表
     * @Author wlxiang
     * @Date 19:26 2023/6/6
     * @Param 
     * @param params
     * @return com.msk.common.core.domain.R<?>
     **/
    @ApiOperation("运距数据列表")
    @PostMapping("/getDistanceList")
    public R<?> getDistanceList(@RequestBody Map<String,Object> params) {
        String affiliatedPersonId = StringUtils.getString(params.get("affiliatedPersonId"));
        if(StringUtils.isBlank(affiliatedPersonId)){
            return R.appFail(400101, "请求参数所属人ID#affiliatedPersonId为空");
        }
        String startNameOrDistance = StringUtils.getString(params.get("startNameOrDistance"));
        List<Map<String,Object>> distanceList = baseDistanceService.selectDistanceList(affiliatedPersonId,startNameOrDistance);
        return R.appOk(distanceList);
    }

    /**
     * @Description 我的运距列表
     * @Author wlxiang
     * @Date 16:11 2023/6/9
     * @Param 
     * @param params
     * @return com.msk.common.core.domain.R<?>
     **/
    @ApiOperation("我的运距列表")
    @PostMapping("/myDistanceList")
    public R<?> myDistanceList(@RequestBody Map<String,Object> params) {
        String affiliatedPersonId = StringUtils.getString(params.get("affiliatedPersonId"));
        if(StringUtils.isBlank(affiliatedPersonId)){
            return R.appFail(400101, "请求参数所属人ID#affiliatedPersonId为空");
        }
        List<Map<String,Object>> distanceList = baseDistanceService.selectMyDistanceList(affiliatedPersonId);
        return R.appOk(distanceList);
    }
}
